package com.example.spinning.controller;

import com.example.spinning.bean.Module;
import com.example.spinning.bean.Organ;
import com.example.spinning.bean.RespBean;
import com.example.spinning.bean.Role;
import com.example.spinning.service.OrganService;
import com.example.spinning.service.RoleService;
import com.example.spinning.service.UserService;
import com.example.spinning.utils.Log;
import com.example.spinning.utils.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色管理
 */

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    RoleService roleService;

//查询全部角色，用于下拉框查询角色
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public List<Role> getAllRoles() {

        List<Role> role_list = roleService.getAllRole();
        return role_list;

    }

    //查询全部角色
    @RequestMapping(value = "/getSpecificValue", method = RequestMethod.GET)
    public Map<String,Object> getRolesByCondition(@RequestParam(value = "page") Integer page,
                                          @RequestParam(value = "count") Integer count,
                                                  Role role) {
        int totalCount = roleService.getRolesCount(role);
        List<Role> role_list = roleService.getAllRole(page,count,role);
        Map<String,Object> map = new HashMap<>();
        map.put("totalCount",totalCount);
        map.put("role",role_list);
        return map;

    }




//    @RequestMapping(value = "/{ids}", method = RequestMethod.DELETE)
//    public RespBean deleteById(@PathVariable String ids) {
//        boolean result = roleService.deleteRoleByIds(ids);
//        if (result) {
//            return new RespBean("success", "删除成功!");
//        }
//        return new RespBean("error", "删除失败!");
//    }
//添加角色
    @Log(module = "角色管理",description = "添加角色")
    @RequestMapping(value = "/add/", method = RequestMethod.POST)
    public RespBean addNewRole(Role role) {
        if ("".equals(role.getRole()) || role.getRole() == null) {
            return new RespBean("error", "请输入角色名称!");
        }
        int result = roleService.addRole(role);

        if (result == 1) {
            return new RespBean("success", "添加成功!");
        }
        if (result == -1) {
            return new RespBean("error", "角色名或英文名重复!");
        }
        if (result == -2) {
            return new RespBean("error", "请选择权限!");
        }
        return new RespBean("error", "添加失败!");
    }

    @RequestMapping(value = "/", method = RequestMethod.PUT)
    public RespBean updateRole(Role role) {
        int i = roleService.updateRoleById(role);
        if (i == 1) {
            return new RespBean("success", "修改成功!");
        }
        return new RespBean("error", "修改失败!");
    }
//修改角色权限
    @Log(module = "角色管理",description = "修改角色")
    @RequestMapping(value = "/priv/update/", method = RequestMethod.PUT)
    public RespBean updateRolePriv(Role role) {
        int result = roleService.updateRolePriv(role);
        if (result == 1) {
            return new RespBean("success", "修改成功!");
        }
        return new RespBean("error", "修改失败!");
    }
//    用角色id查询角色的权限
    @RequestMapping(value = "/priv/getPriv/", method = RequestMethod.GET)
    public Map<String,Object> getRolePriv(String id) {
        List<Module> role_list=roleService.getRolePrivs(id);
        Map<String,Object> map = new HashMap<>();
        map.put("rolePriv",role_list);
        return map;

    }

//  删除角色
    @Log(module = "角色管理",description = "删除角色")
    @RequestMapping(value = "/delete/", method = RequestMethod.DELETE)
    public RespBean deleteRolesById(String ids) {
        boolean result = roleService.deleteRoleByIds(ids);
        if (result) {
            return new RespBean("success", "删除成功!");
        }
        return new RespBean("error", "删除失败!");
    }


}
